<?php
/**购物车模型
 * Created by PhpStorm.
 * User: xiangpeng
 * Date: 2017/1/9
 * Time: 20:25
 */

namespace Common\Model;


class CartModel extends BaseModel
{
    //主键
    protected   $pk   = 'caid';
    //要操作那张表
    protected   $tableName  =  'cart';
    //自动验证
    protected  $_validate   = [

    ];

    //添加方法
    public function addCart(){
        //1.获得session购物车数据
        $cartData  =$_SESSION['cart'];
        //dd($cartData);
        //2.3判断是否重添加
        $temp  = [];
        foreach ($cartData['goods'] as $k=>$v){
            $cartData['goods'][$k]['num']=$v['num'];

        }
        dd($cartData['goods']);
        //if(){
        //
        //}
        //2.重组添加数据
        foreach ($cartData['goods'] as $k=>$v){
                $data  = [
                    'gid'   =>     $v['id'],
                    'num'   =>      $v['num'],
                    'gname'  =>     $v['name'],
                    'price'  =>     $v['price'],
                    'total'  =>     $v['total'],
                    'uid'    =>     session('home.uid'),
                    'options'  =>     implode(',',$v['options']),

                ];
                //执行添加动作
                $this->add($data);

        }

        return ['status'=>'success','message'=>'添加成功'];


    }
}